using QGameFramework.Enums;
using UnityEditor;

public class SetNetWorkEnum : Editor
{
    // 用于存储网络类型的 Key
    private static readonly string NetworkPrefKey = "SelectedNetworkEnum";

    // 让 m_CurrentNetWorkEnum 通过 EditorPrefs 进行持久化存储
    public static NetworkEnum m_CurrentNetWorkEnum
    {
        get => (NetworkEnum)EditorPrefs.GetInt(NetworkPrefKey, (int)NetworkEnum.None);
        set => EditorPrefs.SetInt(NetworkPrefKey, (int)value);
    }

    // 更新 Unity 宏定义
    private static void UpdateScriptingDefineSymbols(string defineSymbol)
    {
        string currentDefines = PlayerSettings.GetScriptingDefineSymbolsForGroup(BuildTargetGroup.Standalone);

        // 清除旧的网络宏定义
        string[] supportedDefines = { "NETWORK_NONE", "NETWORK_TCP", "NETWORK_UDP", "NETWORK_WEBSOCKET" };
        foreach (var supportedDefine in supportedDefines)
        {
            if (currentDefines.Contains(supportedDefine))
            {
                currentDefines = currentDefines.Replace(supportedDefine, "").Replace(";;", ";").Trim(';');
            }
        }

        // 添加新的宏定义
        if (!string.IsNullOrEmpty(defineSymbol))
        {
            currentDefines = string.IsNullOrEmpty(currentDefines) ? defineSymbol : $"{currentDefines};{defineSymbol}";
        }

        // 应用新的宏定义
        PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.Standalone, currentDefines);
    }

    // ========== None ==========  
    [MenuItem("NetWorkEnum/None", false, 0)]
    public static void SetNetWorkNone()
    {
        m_CurrentNetWorkEnum = NetworkEnum.None;
        UpdateScriptingDefineSymbols("NETWORK_NONE");
    }

    [MenuItem("NetWorkEnum/None", true)]
    public static bool ValidateNetWorkNone()
    {
        Menu.SetChecked("NetWorkEnum/None", m_CurrentNetWorkEnum == NetworkEnum.None);
        return true;
    }

    // ========== TCP ==========  
    [MenuItem("NetWorkEnum/TCP", false, 1)]
    public static void SetNetWorkTCP()
    {
        m_CurrentNetWorkEnum = NetworkEnum.TCP;
        UpdateScriptingDefineSymbols("NETWORK_TCP");
    }

    [MenuItem("NetWorkEnum/TCP", true)]
    public static bool ValidateNetWorkTCP()
    {
        Menu.SetChecked("NetWorkEnum/TCP", m_CurrentNetWorkEnum == NetworkEnum.TCP);
        return true;
    }

    // ========== UDP ==========  
    [MenuItem("NetWorkEnum/UDP", false, 2)]
    public static void SetNetWorkUDP()
    {
        m_CurrentNetWorkEnum = NetworkEnum.UDP;
        UpdateScriptingDefineSymbols("NETWORK_UDP");
    }

    [MenuItem("NetWorkEnum/UDP", true)]
    public static bool ValidateNetWorkUDP()
    {
        Menu.SetChecked("NetWorkEnum/UDP", m_CurrentNetWorkEnum == NetworkEnum.UDP);
        return true;
    }

    // ========== WebSocket ==========  
    [MenuItem("NetWorkEnum/WebSocket", false, 3)]
    public static void SetNetWorkWebSocket()
    {
        m_CurrentNetWorkEnum = NetworkEnum.WebSocket;
        UpdateScriptingDefineSymbols("NETWORK_WEBSOCKET");
    }

    [MenuItem("NetWorkEnum/WebSocket", true)]
    public static bool ValidateNetWorkWebSocket()
    {
        Menu.SetChecked("NetWorkEnum/WebSocket", m_CurrentNetWorkEnum == NetworkEnum.WebSocket);
        return true;
    }
}
